package cn.imvc.app.models;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import com.puff.cache.redis.Redis;

import cn.imvc.app.domains.OrderType;
import cn.imvc.app.entities.Member;
import cn.imvc.app.entities.MemberRed;
import cn.imvc.app.entities.Red;
import cn.imvc.app.repositories.MemberRedRepository;
import cn.imvc.data.domain.ViewRequest;
import cn.imvc.ipay.CommonWx;
import cn.imvc.ipay.Payment;
import cn.imvc.util.RandomId;
import me.hao0.wepay.core.Wepay;

@Service
public class MemberRedModelImpl implements MemberRedModel {

	@Autowired
	private MemberRedRepository memberRedRepository;

	@Autowired
	Payment payment;

	public MemberRed get(int id) {
		return memberRedRepository.findOne(id);
	}

	public Page<MemberRed> find(ViewRequest viewRequest) {
		return (Page<MemberRed>) memberRedRepository.findAll();
	}

	public MemberRed save(MemberRed memberRed) {

		MemberRed memberRed2 = memberRedRepository.saveAndFlush(memberRed);

		return memberRed2;
	}

	public MemberRed update(MemberRed newMemberRed) {
		MemberRed loadMemberRed = get(newMemberRed.getId());
		BeanUtils.copyProperties(newMemberRed, loadMemberRed, new String[] { "id" });

		return memberRedRepository.saveAndFlush(loadMemberRed);
	}

	public void delete(int id) {
		memberRedRepository.delete(id);

	}

	public void deleteBatch(int[] ids) {

		for (int id : ids) {
			delete(id);
		}

	}

	public List<MemberRed> findAllRed(Integer memberId) {
		return memberRedRepository.findByMemberIdAndStateOrderByCreateTimeDesc(memberId,1);
	}

	public Map<String, Object> sendRed(Member member, Red red) {

		Map<String, Object> resultMap = new HashMap<String, Object>();

		List<MemberRed> list = memberRedRepository.findByMemberIdAndRedId(member.getId(), red.getId());
		if (list.size() > 0) {
			resultMap.put("code", "0");
			resultMap.put("msg", "您已抢过了");
			return resultMap;
		}
		if (Redis.getInstance().setnx("RedId" + red.getId(), "aa") != 1) {
			resultMap.put("code", "-2");
			resultMap.put("msg", "过于频繁");
			return resultMap;
		}
		MemberRed memberRed = null;
		String code = OrderType.Red.getValue() + RandomId.randomId(new Date().getTime());
		try {
			if (red.getMemberRed().size() >= red.getNum()) {
				Redis.getInstance().remove("RedId" + red.getId());
				resultMap.put("code", "3");
				resultMap.put("msg", "很遗憾，红包已发完");
				return resultMap;
			}
			
			memberRed = new MemberRed();
			memberRed.setCode(code);
			memberRed.setCreateTime(new Date());
			memberRed.setMember(member);
			memberRed.setOpenId(member.getWxOpenid());
			memberRed.setRed(red);
			memberRed.setState(0);
			memberRed = save(memberRed);
		} catch (Exception e) {
			Redis.getInstance().remove("RedId" + red.getId());
			resultMap.put("code", "-2");
			resultMap.put("msg", "过于频繁");
			return resultMap;
		}
		
		resultMap.put("code", "1");
		resultMap.put("msg", "成功");
		resultMap.put("billcode", code);
		resultMap.put("memberRed",memberRed);
		resultMap.put("memberRedId",memberRed.getId());
		return resultMap;
	}
	
}
